/**
 * Created by dukes on 2016/3/4.
 */
class GuildDonatePanel extends eui.Component{
    public constructor(){
        super();
        this.skinName = "skins.scene.GuildDonateSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE,this.onRemoved, this );
    }

    private onAdded(evt:egret.Event){
        this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_ok.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onOk, this);
        this.donate_money.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onDonateMoney, this);
        this.donate_diamon.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onDonateDiamond, this);
        this.updatePageDisplay();
    }

    private onRemoved(evt:egret.Event){
        this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_ok.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onOk, this);
        this.donate_money.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onDonateMoney, this);
        this.donate_diamon.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onDonateDiamond, this);
    }
    public onClose(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.hide();
    }

    public onOk(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.hide();
    }

    public onDonateMoney(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( Model.userExt.donateHairTimes >= Model.userExt.donateHairMaxTimes){
            ShowHint("无捐献次数");
            return;
        }
        if( (Model.me.myType == Model.GOOD_TYPE_DOG && Model.me.dogHair >= 1000000) || (Model.me.myType == Model.GOOD_TYPE_CAT && Model.me.catHair >= 1000000)){
            window["__donate"](0);
        }else{
            ShowHint("毛发不足");
        }

    }

    public onDonateDiamond(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( Model.userExt.donateDiamondTimes >= Model.userExt.donateDiamondMaxTimes){
            ShowHint("无捐献次数");
            return;
        }
        if( Model.me.diamond >= 40){
            window["__donate"](1);
        }else{
            ShowHint("钻石不足");
        }
    }

    public updatePageDisplay(){
        this.money_cnt.text = Model.userExt.donateHairTimes+"/"+Model.userExt.donateHairMaxTimes;
        this.diamond_cnt.text = Model.userExt.donateDiamondTimes+"/"+Model.userExt.donateDiamondMaxTimes;
    }

    public money_cnt:eui.Label;
    public diamond_cnt:eui.Label;
    public donate_money:eui.Image;
    public donate_diamon:eui.Image;
    public img_ok:eui.Image;
    public img_close:eui.Image;
    public partAdded(name:string, instance:any) {
        super.partAdded(name, instance);
        if (name == "img_close") {
            this.img_close = instance;
        } else if (name == "money_cnt") {
            this.money_cnt = instance;
        }else if (name == "img_ok") {
            this.img_ok = instance;
        }else if (name == "diamond_cnt") {
            this.diamond_cnt = instance;
        }else if (name == "donate_money") {
            this.donate_money = instance;
        }else if (name == "donate_diamon") {
            this.donate_diamon = instance;
        }
    }
    public show() {
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
    }
    public hide(){
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);
    }

    public measure(){
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }

    private static _instance:GuildDonatePanel;
    public static getInstance():GuildDonatePanel{
        if( GuildDonatePanel._instance == null)
            GuildDonatePanel._instance = new GuildDonatePanel();
        return GuildDonatePanel._instance;
    }
}